import { Button, Color, Label, Node, Sprite, _decorator } from 'cc';
import { EventMgr } from '../../../Framework/Scripts/Managers/EventMgr';
import { ResMgr } from '../../../Framework/Scripts/Managers/ResMgr';
import { SoundMgr } from '../../../Framework/Scripts/Managers/SoundMgr';
import { UIComponent, UIMgr } from '../../../Framework/Scripts/Managers/UIMgr';
import myUtil from '../../../Framework/Scripts/Utils/myUtil';
import { constant } from '../constant';


import { GameApp } from '../GameApp';
import { UserData } from '../userData/UserData';

const { ccclass, property } = _decorator;


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass('qiandao_UICtrl')
export class qiandao_UICtrl extends UIComponent {
    private qd = [
        { num: 100, type: 1, name: "宝石" },
        { num: 150, type: 1, name: "宝石" },
        { num: 200, type: 1, name: "宝石" },
        { num: 500, type: 2, name: "金币" },
        { num: 600, type: 2, name: "金币" },
        { num: 850, type: 2, name: "金币" },
        { num: 1200, type: 2, name: "金币" },

     


    ]

    onLoad() {

        this.AddButtonListener("close_btn", this, () => {
            GameApp.Instance.isBack = false
            UIMgr.Instance.DestroyUIView(this.node.name)
        })

    }
    start() {
        this.init_qiandao_data();
    }

    init_qiandao_data() {
        const today: number = myUtil.get_dayOfWeek()
        const qiandao = UserData.Instance.localData["qiandao"];
        const item_arr: Node[] = this.ViewNode("center_bg/qiandao_node").children;

        // 加载当前可签到的项

        for (let i = 0; i < item_arr.length; i++) {
            // const num=i+1
            // const item: Sprite = item_arr[i].getComponent(Sprite);
            if (qiandao[i]["num"] < 7) {
                item_arr[i].getChildByName("index").getComponent(Label).string = "星期" + qiandao[i]["num"]
            } else {
                item_arr[i].getChildByName("index").getComponent(Label).string = "星期日"
            }

            item_arr[i].getChildByName("num").getComponent(Label).string = "" + this.qd[i].num
            //let num = i + 1;
            if (qiandao[i]["num"] == today) {
                item_arr[i].getChildByName("bg04").active = true
                if (qiandao[i]["flag"]) {
                    //如果已经签到
                    //  item.color = new Color(255, 255, 255, 255);
                    item_arr[i].getChildByName("isPass").active = true
                    item_arr[i].on("click", () => {
                        GameApp.Instance.show_tip("今日已签到，明日再试!");
                    }, this)
                } else {
                    //如果没签到 
                    //    item.color = new Color(230, 230, 230, 230);
                    item_arr[i].getChildByName("isPass").active = false
                    item_arr[i].on("click", () => {
                        if (UserData.Instance.localData["qiandao"][i]["flag"]) {

                            GameApp.Instance.show_tip("今日已签到，明日再试!");

                        } else {
                  
                            SoundMgr.Instance.playSound(ResMgr.Instance.getAsset("Sounds", "sell"))

                            UserData.Instance.localData["qiandao"][i]["flag"] = true
                            switch (this.qd[i].type) {
                                case 1:
                                    UserData.Instance.localData["ore"] += this.qd[i].num
                                    break;

                                case 2:
                                    UserData.Instance.localData["gold"] += this.qd[i].num
                                    break;

                                default:
                                    break;
                            }

                            UserData.Instance.saveLocalData()
                            item_arr[i].getChildByName("isPass").active = true

                            EventMgr.Instance.Emit(constant.EVENT_NAME.upHomeTop, null)

                     




                        }



                    }, this)
                }




            } else {
                item_arr[i].getChildByName("bg04").active = false
                //把除了今天的UI都为flase
                if (today == 1) {

                    item_arr[i].getChildByName("isPass").active = false
                    UserData.Instance.localData["qiandao"][i]["flag"] = false
                    UserData.Instance.saveLocalData()
                } else {


                    if (UserData.Instance.localData["qiandao"][i]["flag"]) {
                        item_arr[i].getChildByName("isPass").active = true
                    } else {
                        item_arr[i].getChildByName("isPass").active = false
                    }
                }


                item_arr[i].getComponent(Button).interactable = false

            }





        }




    }






    update(deltaTime: number) {

    }
}


